/*本模块功能:学生能力设置
        编写者:杨锡君
        编写时间:20180820*/
$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();
    loading1.init();
    var confirm1 = new Confirm();
    function defaultModel(courseStr) {
        var str = '<li>' +
            '<div class="titleTop">' +
            '<div class="fl showTitle">' +
            '<i class="fl powerName"></i><img class="fl editAbility" src="pc/images/ico_weightEdit.png" alt="">' +
            '</div>' +
            '<div class="fl editTitle">' +
            '<input type="text" maxlength="10" class="publicText fl"><img class="fl saveAbility" src="pc/images/ico_weightSave.png" alt="">' +
            '</div>' +
            '<div class="btnBox fr">' +
            '<img src="pc/images/ico_weightAdd1.png" class="fl weightAdd" alt="">' +
            '<img src="pc/images/ico_weightDel2.png" class="fl" alt="">' +
            '<img src="pc/images/ico_weightSelect.png" data-value="0" class="fl weightSelect" alt="">' +
            '</div>' +
            '<div class="processBox fr">' +
            '<div class="noneShare">剩余权重100%</div>' +
            '<div class="processBg">' +
            '<div class="processTop"></div>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '<ul class="courseList">' +
            '<li>' +
            '<div class="fl inputRight"><label>课程名称：&ensp;</label>' +
            '<select class="publicSelect courseName">' +
            '<option value="0">请选择</option>' +
            courseStr+
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label>权重分配：&ensp;</label>' +
            '<select class="publicSelect abilitySet">' +
            '<option value="0">请选择</option>' +
            '<option value="1">100%</option>' +
            '<option value="0.9">90%</option>' +
            '<option value="0.8">80%</option>' +
            '<option value="0.7">70%</option>' +
            '<option value="0.6">60%</option>' +
            '<option value="0.5">50%</option>' +
            '<option value="0.4">40%</option>' +
            '<option value="0.3">30%</option>' +
            '<option value="0.2">20%</option>' +
            '<option value="0.1">10%</option>' +
            '</select>' +
            '</div>' +
            '<div class="fl inputRight">' +
            '<i class="warningInfo"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">该权重值超出可用范围</i></i>' +
            '</div>' +
            '<div class="btnBox fr">' +
            '<img src="pc/images/ico_weightAdd2.png" class="fl weightAdds" alt="">' +
            '<img src="pc/images/ico_weightDel4.png" class="fl" alt="">' +
            '</div>' +
            '</li>' +
            '<i class="warningInfo warnOut" style="margin-top: 10px;margin-left: 0;"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">存在未分配的权重</i></i>' +
            '</ul>' +
            '</li>';
        return str;
    }
    function addModel(courseStr) {
        var str = '<li>' +
            '<div class="titleTop">' +
            '<b style="line-height: 74px;" class="fl">能力名称：</b>'+
            '<div class="fl showTitle">' +
            '<i class="fl powerName"></i><img class="fl editAbility" src="pc/images/ico_weightEdit.png" alt="">' +
            '</div>' +
            '<div class="fl editTitle">' +
            '<input type="text" maxlength="10"  class="publicText fl"><img class="fl saveAbility" src="pc/images/ico_weightSave.png" alt="">' +
            '</div>' +
            '<div class="btnBox fr">' +
            '<img src="pc/images/ico_weightAdd1.png" class="fl weightAdd" alt="">' +
            '<img src="pc/images/ico_weightDel1.png" class="fl weightDel" alt="">' +
            '<img src="pc/images/ico_weightSelect.png" data-value="0" class="fl weightSelect" alt="">' +
            '</div>' +
            '<div class="processBox fr">' +
            '<div class="noneShare">剩余权重100%</div>' +
            '<div class="processBg">' +
            '<div class="processTop"></div>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '<ul class="courseList">' +
            '<li>' +
            '<div class="fl inputRight"><label>课程名称：&ensp;</label>' +
            '<select class="publicSelect courseName">' +
            '<option value="0">请选择</option>' +
            courseStr+
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label>权重分配：&ensp;</label>' +
            '<select class="publicSelect abilitySet">' +
            '<option value="0">请选择</option>' +
            '<option value="1">100%</option>' +
            '<option value="0.9">90%</option>' +
            '<option value="0.8">80%</option>' +
            '<option value="0.7">70%</option>' +
            '<option value="0.6">60%</option>' +
            '<option value="0.5">50%</option>' +
            '<option value="0.4">40%</option>' +
            '<option value="0.3">30%</option>' +
            '<option value="0.2">20%</option>' +
            '<option value="0.1">10%</option>' +
            '</select>' +
            '</div>' +
            '<div class="fl inputRight">' +
            '<i class="warningInfo"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">该权重值超出可用范围</i></i>' +
            '</div>' +
            '<div class="btnBox fr">' +
            '<img src="pc/images/ico_weightAdd2.png" class="fl weightAdds" alt="">' +
            '<img src="pc/images/ico_weightDel3.png" class="fl weightDels" alt="">' +
            '</div>' +
            '</li>' +
            '<i class="warningInfo warnOut" style="margin-top: 10px;margin-left: 0;"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">存在未分配的权重</i></i>' +
            '</ul>' +
            '</li>';
        return str;
    }
    function addCourseModel(courseStr) {
        return '<li>' +
            '<div class="fl inputRight"><label>课程名称：&ensp;</label>' +
            '<select class="publicSelect courseName">' +
            '<option value="0">请选择</option>' +
            courseStr+
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label>权重分配：&ensp;</label>' +
            '<select class="publicSelect abilitySet">' +
            '<option value="0">请选择</option>' +
            '<option value="1">100%</option>' +
            '<option value="0.9">90%</option>' +
            '<option value="0.8">80%</option>' +
            '<option value="0.7">70%</option>' +
            '<option value="0.6">60%</option>' +
            '<option value="0.5">50%</option>' +
            '<option value="0.4">40%</option>' +
            '<option value="0.3">30%</option>' +
            '<option value="0.2">20%</option>' +
            '<option value="0.1">10%</option>' +
            '</select>' +
            '</div>' +
            '<div class="fl inputRight">' +
            '<i class="warningInfo"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">该权重值超出可用范围</i></i>' +
            '</div>' +
            '<div class="btnBox fr">' +
            '<img src="pc/images/ico_weightAdd2.png" class="fl weightAdds" alt="">' +
            '<img src="pc/images/ico_weightDel3.png" class="fl weightDels" alt="">' +
            '</div>' +
            '</li>';
    }
    function editModel(courseStr,opt) {
        var str = '';
        for(var i = 0;i < opt.length;i++){
            var wid = 0;
            if(opt[i].courseList.length!=0){
                for(var j = 0;j < opt[i].courseList.length;j++){
                    wid+=opt[i].courseList[j].width*100;
                }
            }
            str += '<li>' +
                '<div class="titleTop">' +
                '<b style="line-height: 74px;" class="fl">能力名称：</b>'+
                '<div class="fl showTitle">' +
                '<i class="fl powerName">'+ opt[i].powerName +'</i><img class="fl editAbility" src="pc/images/ico_weightEdit.png" alt="">' +
                '</div>' +
                '<div class="fl editTitle">' +
                '<input type="text" maxlength="10"  class="publicText fl"><img data-id="'+ opt[i].powerId +'" class="fl saveAbility" src="pc/images/ico_weightSave.png" alt="">' +
                '</div>' +
                '<div class="btnBox fr">' +
                '<img src="pc/images/ico_weightAdd1.png" class="fl weightAdd" alt="">' +
                '<img data-id="'+ opt[i].powerId +'" src="pc/images/ico_weightDel1.png" class="fl weightDel" alt="">' +
                '<img src="pc/images/ico_weightSelect.png" data-value="0" class="fl weightSelect" alt="">' +
                '</div>' +
                '<div class="processBox fr">' +
                '<div class="noneShare">剩余权重'+ (100-wid) +'%</div>' +
                '<div class="processBg">' +
                '<div style="width: '+ wid +'%;" class="processTop"></div>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<ul class="courseList">';
            if(opt[i].courseList.length!=0){
                for(var j = 0;j < opt[i].courseList.length;j++){
                    str+='<li>' +
                        '<div class="fl inputRight"><label>课程名称：&ensp;</label>' +
                        '<select data-value="'+ opt[i].courseList[j].courseId +'" class="publicSelect courseName">' +
                        '<option value="0">请选择</option>' +
                        courseStr+
                        '</select>' +
                        '</div>' +
                        '<div class="fl inputRight"><label>权重分配：&ensp;</label>' +
                        '<select data-value="'+ opt[i].courseList[j].width +'" class="publicSelect abilitySet">' +
                        '<option value="0">请选择</option>' +
                        '<option value="1">100%</option>' +
                        '<option value="0.9">90%</option>' +
                        '<option value="0.8">80%</option>' +
                        '<option value="0.7">70%</option>' +
                        '<option value="0.6">60%</option>' +
                        '<option value="0.5">50%</option>' +
                        '<option value="0.4">40%</option>' +
                        '<option value="0.3">30%</option>' +
                        '<option value="0.2">20%</option>' +
                        '<option value="0.1">10%</option>' +
                        '</select>' +
                        '</div>' +
                        '<div class="fl inputRight">' +
                        '<i class="warningInfo"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">该权重值超出可用范围</i></i>' +
                        '</div>' +
                        '<div class="btnBox fr">' +
                        '<img src="pc/images/ico_weightAdd2.png" class="fl weightAdds" alt="">' +
                        '<img data-courseid="'+ opt[i].courseList[j].courseId +'" src="pc/images/ico_weightDel3.png" class="fl weightDels" alt="">' +
                        '</div>' +
                        '</li>';
                }
            }else{
                str+='<li>' +
                    '<div class="fl inputRight"><label>课程名称：&ensp;</label>' +
                    '<select class="publicSelect courseName">' +
                    '<option value="0">请选择</option>' +
                    courseStr+
                    '</select>' +
                    '</div>' +
                    '<div class="fl inputRight"><label>权重分配：&ensp;</label>' +
                    '<select class="publicSelect abilitySet">' +
                    '<option value="0">请选择</option>' +
                    '<option value="1">100%</option>' +
                    '<option value="0.9">90%</option>' +
                    '<option value="0.8">80%</option>' +
                    '<option value="0.7">70%</option>' +
                    '<option value="0.6">60%</option>' +
                    '<option value="0.5">50%</option>' +
                    '<option value="0.4">40%</option>' +
                    '<option value="0.3">30%</option>' +
                    '<option value="0.2">20%</option>' +
                    '<option value="0.1">10%</option>' +
                    '</select>' +
                    '</div>' +
                    '<div class="fl inputRight">' +
                    '<i class="warningInfo"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">该权重值超出可用范围</i></i>' +
                    '</div>' +
                    '<div class="btnBox fr">' +
                    '<img src="pc/images/ico_weightAdd2.png" class="fl weightAdds" alt="">' +
                    '<img src="pc/images/ico_weightDel3.png" class="fl weightDels" alt="">' +
                    '</div>' +
                    '</li>';


            }
                str+='<i class="warningInfo warnOut" style="margin-top: 10px;margin-left: 0;"><img src="pc/images/ico_weightWarn.png" alt=""><i class="fl">存在未分配的权重</i></i></ul>' +
                '</li>';
        }

        return str;
    }
    var weightSet = {
      init:function () {
          this.bindEvent();
      },
      bindEvent:function () {
          var self = this;
          self.abilitySetEvent();
          $(".abilityBtn").off("click").click(function(){
                $(".abilitySection").show();
                $(".courseSection").hide();
                $(this).addClass("liCheck");
              $(".courseBtn").removeClass("liCheck");
              self.abilitySetEvent();
          });
          $(".courseBtn").off("click").click(function(){
              $(".abilitySection").hide();
              $(".courseSection").show();
              $(this).addClass("liCheck");
              $(".abilityBtn").removeClass("liCheck");
              self.courseSetEvent();
          });
      },
        abilitySetEvent:function () {
            var self = this;
            $.ajax({
                type: 'post',
                url: 'getAllCourseWithCoursePower',
                async: true,
                success: function (msg) {
                    msg = JSON.parse(msg);
                    var courseStr = '';
                    if(msg.courseList!=undefined&&msg.courseList!=''&&msg.courseList!=null&&msg.courseList.length!=0){
                        $.map(msg.courseList,function (opt) {
                            courseStr+='<option value="'+ opt.courseId +'">'+ opt.courseName +'</option>';
                        });
                    }
                    //获取能力列表
                    $.ajax({
                        type: 'post',
                        url: 'skipCoursePowerMana',
                        async: true,
                        data:{
                            "jsonStr":JSON.stringify({"pageNow":1,"pageSize":10})
                        },
                        success: function (res) {
                            res = JSON.parse(res);
                            if (res.result == "success") {
                                if(res.coursePowerList!=undefined&&res.coursePowerList!=''&&res.coursePowerList!=null&&res.coursePowerList.length!=0){
                                    var str = editModel(courseStr,res.coursePowerList);
                                    $(".abilityList").html(str);
                                    $.each($("select"),function (opt) {
                                        if($(this).attr("data-value")!=undefined){
                                            $(this).val($(this).attr("data-value"));
                                        }
                                    });
                                }else{
                                    var str = addModel(courseStr);
                                    $(".abilityList").html(str);
                                }
                                self.someEvent(courseStr);
                                maxLengthLimit.init("publicText");
                            }else{
                                Alert1.content = res.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                }
            });
            //能力提交
            $(".saveAbilitySet").off("click").click(function () {
                var flag = true;

                $.each($(".abilityList>li"),function (opt) {
                    if($(this).find(".powerName").html()==''){
                        flag = false;
                        Alert1.content = "能力名称不能为空";
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }
                });
                if(flag){
                    $.each($(".courseName"),function (opt) {
                        if($(this).val()==0){
                            flag = false;
                            Alert1.content = "请将课程信息补充完整";
                            Alert1.init();
                            Alert1.show();
                            return false;
                        }
                    });
                }
                if(flag){
                    $.each($(".abilityList>li"),function (opt) {
                        if($(this).find(".noneShare").html()!="剩余权重0%"){
                            flag = false;
                            $(this).find(".warnOut").css("display","inline-block");
                            return false;
                        }else{
                            $(this).find(".warnOut").hide();
                        }
                    });
                }

                if(flag){
                    var arr = [];
                    for(var i = 0;i < $(".abilityList>li").length;i++){
                        var array = [];
                        var obj = {};
                        obj.powerId = Number($(".abilityList>li").eq(i).find(".saveAbility").attr("data-id"));
                        obj.powerName = $(".abilityList>li").eq(i).find(".powerName").html();
                        if($(".abilityList>li").eq(i).find(".courseList>li").length!=0){
                            for(var j = 0;j < $(".abilityList>li").eq(i).find(".courseList>li").length;j++){
                                array.push({"courseId":Number($(".abilityList>li").eq(i).find(".courseList>li").eq(j).find(".courseName").val()),"width":Number($(".abilityList>li").eq(i).find(".courseList>li").eq(j).find(".abilitySet").val())});
                            }
                            obj.addRelCourseList = array;
                        }
                        if($(".abilityList>li").eq(i).attr("data-list")!=undefined){
                            var ars = $(".abilityList>li").eq(i).attr("data-list").split(",");
                            var ar = [];
                            for(var i =0;i<ars.length;i++){
                                ar.push(Number(ars[i]));
                            }
                            obj.delRelCourseList = ar;
                        }
                        arr.push(obj);
                    }
                    var json = JSON.stringify({"powerList":arr});
                    $.ajax({
                        type: 'post',
                        url: 'saveCoursePower',
                        async: true,
                        data: {
                            "jsonStr": json
                        },
                        success: function (msg) {
                            msg = JSON.parse(msg);
                            if (msg.result == "success") {
                                Alert1.content = "保存成功";
                                Alert1.success = function () {
                                  window.location.reload(true);
                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                }
            });
        },
        someEvent:function (courseStr) {
          var self = this;
          // 收缩栏
            $(".weightSelect").off("click").click(function () {
               if($(this).attr("data-value") == 0){
                   $(this).attr("data-value",1);
                   $(this).parent().parent().siblings("ul").children("li").hide();
                   $(this).css("transform", "rotate(180deg)");
               }else{
                   $(this).attr("data-value",0);
                   $(this).parent().parent().siblings("ul").children("li").show();
                   $(this).css("transform", "rotate(360deg)");
               }
            });
          // 权重改变
            $(".abilitySet").off("change").change(function () {
                var that = $(this);
                var count = 0;
                $.each(that.parent().parent().parent().children("li"),function (opt) {
                    if($(this).find(".abilitySet").val() != 0){
                        count += Number($(this).find(".abilitySet").val())*10;
                    }

                });
                if(count>10){
                    that.val(0);
                    that.parent().parent().find(".warningInfo").css("display","inline-block");
                }else{
                    that.parent().parent().find(".warningInfo").hide();
                }
                count = 0;
                $.each(that.parent().parent().parent().children("li"),function (opt) {
                    if($(this).find(".abilitySet").val() != 0){
                        count += Number($(this).find(".abilitySet").val())*10;
                    }

                });
                that.parent().parent().parent().parent().find(".processTop").css("width",count*10+"%");
                that.parent().parent().parent().parent().find(".noneShare").html("剩余权重"+(10-count)*10+"%");
            });
          //添加课程
            $(".weightAdds").off("click").click(function () {
                var str = addCourseModel(courseStr);
                $(this).parent().parent().after(str);
                self.someEvent(courseStr);
                maxLengthLimit.init("publicText");
            });
            //删除课程
            $(".weightDels").off("click").click(function () {
                if($(this).attr("data-courseid")!=undefined){
                    if($(this).parent().parent().parent().parent().attr("data-list")!=undefined){
                        var arr  = [];
                        arr.push($(this).parent().parent().parent().parent().attr("data-list").split(","));
                        arr.push($(this).attr("data-courseid"));
                        $(this).parent().parent().parent().parent().attr("data-list",arr);
                    }else{
                        var arr = [];
                        arr.push($(this).attr("data-courseid"));
                        $(this).parent().parent().parent().parent().attr("data-list",arr);
                    }
                }
                if($(this).parent().parent().parent().children("li").length == 1){
                    var str = addCourseModel(courseStr);
                    $(this).parent().parent().after(str);
                    self.someEvent(courseStr);
                }
                $(this).parent().parent().remove();
                for(var i = 0;i < $(".courseList").length;i++){
                    var count = 0;
                    for(var j = 0;j < $(".courseList").eq(i).children("li").length;j++){
                        if($(".courseList").eq(i).children("li").eq(j).find(".abilitySet").val()!=0){
                            count += Number($(".courseList").eq(i).children("li").eq(j).find(".abilitySet").val())*10;
                        }
                    }
                    $(".courseList").eq(i).parent().find(".processTop").css("width",count*10+"%");
                    $(".courseList").eq(i).parent().find(".noneShare").html("剩余权重"+(10-count)*10+"%");
                }



            });
            //添加能力
            $(".weightAdd").off("click").click(function () {
                if($(".weightAdd").length<6){
                    var str = addModel(courseStr);
                    $(".abilityList").append(str);
                    self.someEvent(courseStr);
                    maxLengthLimit.init("publicText");
                }else{
                    Alert1.content = "最多添加6个能力";
                    Alert1.init();
                    Alert1.show();
                }

            });
            //删除能力
            $(".weightDel").off("click").click(function () {
                if($(this).attr("data-id")!=undefined){
                    var that = $(this);
                    $.ajax({
                        type: 'post',
                        url: 'delCoursePowers',
                        data:{
                            "jsonStr":JSON.stringify([that.attr("data-id")])
                        },
                        async: true,
                        success: function (msg) {
                            msg = JSON.parse(msg);
                            if (msg.result == "success") {
                                if($(".abilityList>li").length == 1){
                                    var str = addModel(courseStr);
                                    $(".abilityList").append(str);
                                    self.someEvent(courseStr);
                                }
                                that.parent().parent().parent().remove();
                            }else{
                                if(msg.failMsg!=undefined){
                                    Alert1.content = msg.failMsg;
                                }else{
                                    Alert1.content = "保存失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                }else{
                    if($(".abilityList>li").length ==1){
                        var str = addModel(courseStr);
                        $(".abilityList").append(str);
                        self.someEvent(courseStr);
                    }
                    $(this).parent().parent().parent().remove();
                }

            });
            // 编辑能力名称
            $(".editAbility").off("click").click(function () {
                var that = $(this);
                var name = that.siblings("i").html();
                that.parent().next().show();
                that.parent().next().children("input").val(name);
                that.parent().hide();
                $(".saveAbility").off("click").click(function () {
                    if($(this).attr("data-id")!=undefined){
                        var val = $(this).siblings("input").val();
                        var powerId = Number($(this).attr("data-id"));
                        if(val == ''){
                            Alert1.content = "能力名称不能为空";
                            Alert1.init();
                            Alert1.show();
                        }else{
                            // $(this).parent().prev().show();
                            // $(this).parent().prev().children("i").html(val);
                            // $(this).parent().hide();
                            var they = $(this);
                            $.ajax({
                                type: 'post',
                                url: 'addCousrPower',
                                data:{
                                    "jsonStr":JSON.stringify({"powerList":[{"powerName":val,"powerId":powerId}]})
                                },
                                async: true,
                                success: function (msg) {
                                    msg = JSON.parse(msg);
                                    if (msg.result == "success") {
                                        they.parent().prev().show();
                                        they.parent().prev().children("i").html(val);
                                        they.parent().next().attr("data-id",msg.powerId);
                                        they.parent().hide();
                                        they.attr("data-id",msg.powerId);
                                        they.parent().parent().find(".weightDel").attr("data-id",msg.powerId);
                                    }else{
                                        if(msg.failMsg!=undefined){
                                            Alert1.content = msg.failMsg;
                                        }else{
                                            Alert1.content = "保存失败";
                                        }
                                        Alert1.init();
                                        Alert1.show();
                                    }
                                }
                            });
                        }

                    }else{
                        var val = $(this).siblings("input").val();
                        if(val == ''){
                            Alert1.content = "能力名称不能为空";
                            Alert1.init();
                            Alert1.show();
                        }else{
                            var they = $(this);
                            $.ajax({
                                type: 'post',
                                url: 'addCousrPower',
                                data:{
                                    "jsonStr":JSON.stringify({"powerList":[{"powerName":val,"powerId":0}]})
                                },
                                async: true,
                                success: function (msg) {
                                    msg = JSON.parse(msg);
                                    if (msg.result == "success") {
                                        they.parent().prev().show();
                                        they.parent().prev().children("i").html(val);
                                        they.parent().next().attr("data-id",msg.powerId);
                                        they.parent().hide();
                                        they.attr("data-id",msg.powerId);
                                        they.parent().parent().find(".weightDel").attr("data-id",msg.powerId);
                                    }else{
                                        if(msg.failMsg!=undefined){
                                            Alert1.content = msg.failMsg;
                                        }else{
                                            Alert1.content = "保存失败";
                                        }
                                        Alert1.init();
                                        Alert1.show();
                                    }
                                }
                            });
                        }
                    }
                });
            });
        },
        courseSetEvent:function () {
            var self = this;
            var arr=[];
            var num = 0;
            //获取课程各个模块的权重
            $.ajax({
                type:'post',
                url:'getCourseWidth',
                async:true,
                success:function (msg) {
                    msg = JSON.parse(msg);
                    if(msg.result == "success"){
                        if(msg.courseWidth!=undefined&&msg.courseWidth!=null&&msg.courseWidth!=''){
                            $("#labWeight").val(msg.courseWidth.expreWidth);
                            $("#workWeight").val(msg.courseWidth.homeworkWidth);
                            $("#testWeight").val(msg.courseWidth.examWidth);
                        }
                        $.each($(".courseSet"),function (opt) {
                            if($(this).val() != 0){
                                num+=Number($(this).val());
                                arr.push({value:Number($(this).val()),name:$(this).attr("data-name"),itemStyle:{
                                        normal:{color:[$(this).attr("data-color")]}
                                    }});
                            }
                        });
                        if(num<1){
                            arr.unshift({value:1-num,name:"未分配",itemStyle:{
                                    normal:{color:'#e5e5e5'}
                                }});
                        }
                        self.courseChart(arr);
                    }else{
                        Alert1.content = msgNew.failMsg;
                        Alert1.init();
                        Alert1.show();
                    }
                },
                error:function (err) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
                }
            });
            // 课程权重改变
            $(".courseSet").off("change").change(function () {
                var that = $(this);
                var count = 0;
                var array = [];
                $.each($(".courseSet"),function (opt) {
                    if($(this).val() != 0){
                        count += Number($(this).val());
                    }

                });
                if(count>1){
                    that.val(0);
                    that.next().css("display","inline-block");
                }else{
                    that.next().hide();
                }
                var num = 0;
                $.each($(".courseSet"),function (opt) {
                    if($(this).val() != 0){
                        num+=Number($(this).val());
                        array.push({value:Number($(this).val()),name:$(this).attr("data-name"),itemStyle:{
                                normal:{color:[$(this).attr("data-color")]}
                            }});
                    }

                });
                if(num<1){
                    array.unshift({value:1-num,name:"未分配",itemStyle:{
                            normal:{color:'#e5e5e5'},
                            emphasis: {
                                show: false
                            }
                        }});
                }
                self.courseChart(array);
            });
            // 课程权重提交
            $(".saveCourseSet").off("click").click(function () {
                var sum = (Number($("#labWeight").val())*10+Number($("#workWeight").val())*10+Number($("#testWeight").val())*10)/10;
                if(sum<1){
                    Alert1.content = "存在未分配权重，请再次分配。";
                    Alert1.init();
                    Alert1.show();
                }else if(sum>1){
                    Alert1.content = "未正确分配权重，请重新分配。";
                    Alert1.init();
                    Alert1.show();
                }else{
                    var json = JSON.stringify({"expreWidth":Number($("#labWeight").val()),"homeworkWidth":Number($("#workWeight").val()),"examWidth":Number($("#testWeight").val())});
                    $.ajax({
                        type: "post",
                        url: "addCourseWidth",
                        async: true,
                        data: {
                            "jsonStr": json
                        },
                        success: function(msg) {
                            loading1.hide();
                            var msg = JSON.parse(msg);
                            if(msg.result == "success") {
                                Alert1.content = "保存成功";
                                Alert1.init();
                                Alert1.show();
                            } else {
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();

                            }

                        },
                        error: function(data, status, xhr) {
                            var json = JSON.stringify({
                                "pageName": "error"
                            });
                            window.location.href = "skipPage?jsonStr=" + json;

                        }
                    });
                }
            });
        } ,
        courseChart:function (arr) {
           var myChart = echarts.init(document.getElementById('circleBox'));
           var option = {
               calculable : true,
               series : [
                   {
                       hoverable:false,
                       dataRangeHoverLink:false,
                       clickable:false,
                       type:'pie',
                       radius : ['40%', '70%'],
                       center: ['50%', '45%'],
                       data:arr
                   }
               ]
           };
           myChart.setOption(option);
        }
    };
    weightSet.init();
});